<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\BusinessException;
use App\Models\AddressModel;
use App\Utils\ControllerModelTrait;
use Hfm\BaiduAi\BaiduAI;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AddressController extends ApiController
{
    use ControllerModelTrait;

    /**
     * 获取地址管理列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getList(Request  $request): JsonResponse
    {
        $Model = AddressModel::where('id', '>', 0);
        if ($request->input('uid'))
            $Model = $Model->where('uid', $request->input('uid'));
        $paginate = $this->getPagedData($Model, $request);
        return $this->successPaginate($paginate);
    }

    /**
     * 保存数据
     * @param Request $request
     * @return JsonResponse
     */
    public function doSave(Request $request): JsonResponse
    {
        $data = $request->only(['uid', 'name', 'mobile', 'province', 'city', 'district', 'address', 'default_flag']);
        $this->saveData(new AddressModel, $data);
        return $this->success();
    }

    /**
     * 删除
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doDelete(): JsonResponse
    {
        $ids = $this->verifyString('ids');
        $this->deleteData(new AddressModel, $ids);
        return $this->success();
    }

    /**
     * 百度AI地址解析
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function aiAddress(Request $request): JsonResponse
    {
        $text = $this->verifyString('text');
        $AI = new BaiduAI('25871049', '9S5kIWFbaGezK5tVhngS3kzA', 'YjZwm6Y0G3oe24ESNe8jKwpbAuBpPQwq');
        $data = $AI->getAddressParams($text);
        return $this->success($data);
    }
}
